<template>
  <el-dialog
    :title="dialogProps.title"
    :visible.sync="dialogProps.visible"
    width="560px"
    @open="handleDialogOpen()"
  >
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div>
    <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
      <el-tab-pane label="基本信息" name="first"></el-tab-pane>
      <el-form
        :model="HealModel"
        :rules="formHeal"
        ref="healForm"
        label-width="80px"
        label-position="right"
        size="small"
      >
        <el-form-item label="方案标题" prop="healTitle">
          <el-col :span="16"
            ><el-input
              ref="healTitle"
              :maxlength="32"
              v-model="HealModel.healTitle"
              autofocus
            ></el-input
          ></el-col>
        </el-form-item>

        <el-form-item label="关键词" prop="healAntistop">
          <el-col :span="16"
            ><el-input
              ref="healAntistop"
              :maxlength="32"
              v-model="HealModel.healAntistop"
            ></el-input
          ></el-col>
        </el-form-item>

        <el-form-item label="方案类别" prop="healType">
          <el-select v-model="HealModel.healType" placeholder="请选择">
            <el-option
              ref="healType"
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="内容" prop="healContent">
          <el-input
            ref="healContent"
            resize="none"
            type="textarea"
            v-model="HealModel.healContent"
          ></el-input>
        </el-form-item>

        <el-form-item label="备注" prop="healRemark">
          <el-input
            ref="healRemark"
            type="textarea"
            resize="none"
            v-model="HealModel.healRemark"
          ></el-input>
        </el-form-item>

        <el-form-item label="状态" prop="healState">
          <el-switch
            v-model="HealModel.healState"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
    </el-tabs>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" size="small" @click="submitForm('healForm')"
        >保 存</el-button
      >
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addHealth, healUpdate } from "@/api/Repository/health";

export default {
  name: "role-edit",
  data() {
    return {
      activeName: "first",

      options: [
        { value: "心理干预", label: "心理干预" },
        { value: "戒烟限酒", label: "戒烟限酒" },
        { value: "饮食干预", label: "饮食干预" },
        { value: "注意事项", label: "注意事项" },
        { value: "运动干预", label: "运动干预" }
      ],

      HealModel: [
        {
          id: "",
          healCoding: "",
          healTitle: "",
          healAntistop: "",
          healType: "",
          healContent: "",
          healRemark: "",
          healState: ""
        }
      ],

      //加载
      locked: false,

      dialogProps: {
        visible: false,
        action: "",
        title: ""
      },
      formHeal: {
        healTitle: [
          { required: true, message: "请输入标题", trigger: "blur" },
          { min: 2, max: 32, message: "长度在 2 到 32 个字符", trigger: "blur" }
        ],
        healAntistop: [
          { required: true, message: "请输入关键词", trigger: "blur" }
        ],
        healType: [
          { required: true, message: "请选择类别", trigger: "change" }
        ],
        healContent: [
          { required: true, message: "请输入内容", trigger: "blur" }
        ]
        // healRemark: [{ required: true, message: "请输入备注", trigger: "blur" }]
      }
    };
  },
  methods: {
    //选项卡点击
    handleClick(tab, event) {
      console.log(tab, event);
    },

    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.dialogProps.action === "add") {
            this.doSaveRole();
          } else {
            this.doUpdateRole();
          }
        } else {
          return false;
        }
      });
    },
    doUpdateRole() {
      healUpdate(this.HealModel)
        .then(response => {
          this.dialogProps.visible = false;
          if (response.data > 0) {
            this.$emit("save-finished");
            this.$message({
              message: "修改成功",
              type: "success"
            });
          }
        })
        .catch(error => {
          this.$message.error("修改失败");
        });
    },
    doSaveRole() {
      addHealth(this.HealModel)
        .then(response => {
          this.locked = false;
          this.dialogProps.visible = false;
          if (response.data > 0) {
            this.$emit("save-finished");
            this.$message({
              message: "添加成功",
              type: "success"
            });
          }
        })
        .catch(error => {
          this.$message.error("添加失败");
        });
    },

    handleDialogClose() {
      this.dialogProps.visible = false;
    },
    handleDialogOpen() {
      this.$nextTick(() => {
        this.$refs["healForm"].clearValidate();
        this.$refs["healTitle"].focus();
      });
    },

    outputError(error) {
      console.log(error.response ? error.response : error.message);
      this.$message({
        showClose: true,
        message: "出错了，请按F12查看浏览器日志。",
        type: "error"
      });
    }
  },
  mounted: function() {
    this.$nextTick(() => {
      this.$on("openEditHealDialog", function(eModel) {
        this.HealModel = eModel;
        this.dialogProps.action = "edit";
        this.dialogProps.title = "编辑";
        this.dialogProps.visible = true;
      });
      this.$on("openAddHealDialog", function() {
        this.dialogProps.action = "add";
        this.dialogProps.title = "新增";
        this.HealModel = {
          healState: "1"
        };
        this.dialogProps.visible = true;
      });
    });
  }
};
</script>

